<template>
  <view class="content">
    <image class="logo" src="/static/logo.png"></image>
    <view class="text-area">      
	  <button @click="modbus" type="default">modbus</button>
	  <button @click="modbusRead" type="default">read</button>
	  <button @click="modbusWrite" type="default">write</button>
    </view>
  </view>
</template>

<script setup>
import { onMounted } from '@vue/runtime-core';
const {SerialPort} = require('serialport');
const ModbusRTU = require("modbus-serial")
const client = new ModbusRTU();

function listenPort(){
SerialPort.list().then((ports,err)=>{
    console.log(ports)    
})
}
function modbus(){
	client.connectRTUBuffered("COM3",{
		baudRate:9600,
		dataBits:8,
		stopBits:1,
		parity:'none'
	})
	.then((res)=>{
		console.log(res,'res')    
	})
	.catch((err)=>{
		console.log(err,'err')
	})
}
function modbusWrite() {
    client.setID(3);
    client.writeRegister(0x000E, 1)
    .then((res)=>{
    console.log(res,'modbusWrite')
    });
}

function modbusRead() {
    client.setID(3);
    client.readHoldingRegisters(0, 3)
    .then(console.log);
}
onMounted(()=>{
	listenPort();	
})
</script>

<style>
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.logo {
  height: 200rpx;
  width: 200rpx;
  margin-top: 200rpx;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 50rpx;
}

.text-area {
  display: flex;
  justify-content: center;
}

.title {
  font-size: 36rpx;
  color: #8f8f94;
}
</style>
